package com.atguigu.demo.controller;

import com.atguigu.demo.event.ProductOnSaleEvent;
import com.atguigu.demo.service.api.ProductService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class ProductController {

    @Resource
    private ProductService productService;

    @Autowired
    private ApplicationEventPublisher publisher;

    @GetMapping("/product/on/sale/{productId}")
    public String productOnSale(@PathVariable("productId") Integer productId) {

        productService.onSale(productId);

        // 后续操作重构：转移到事件监听回调函数中执行
//        productService.putIntoCache(productId);
//        productService.putIntoSearch(productId);
//        productService.putIntoAdQueue(productId);
//        productService.pushToGuest(productId);

        log.debug("[主体任务] threadName = " + Thread.currentThread().getName());

        // 发布事件
        publisher.publishEvent(new ProductOnSaleEvent(productId));

        return "product on sale success [event]";
    }

}